﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhSystemBLL.orderData;
using baseSet.importExport;
using dhDataAcess.linqDAL;
using System.Data.SqlClient;
using dhsystem.Main;
using dhSystemBLL.basicData;
using FastReport;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using baseSet.power;

namespace dhsystem.orderForm
{
    public partial class FrmProductList : Form
    {
        productUnitBLL productUnit = new productUnitBLL();
        productTypeBLL productType = new productTypeBLL();
        productListBLL product = new productListBLL();
        string state;
        public FrmProductList()
        {
            InitializeComponent();
        }
        //新增按钮，显示新增窗口
        private void toolAdd_Click(object sender, EventArgs e)
        {
             state = "新增";
            FrmProductAdd productAdd = new FrmProductAdd(bindingSourceProductList,state);
            productAdd.ShowDialog();  
        }
        //打开窗口时显示列表数据
        private void FrmProductList_Load(object sender, EventArgs e)
        {

            bindingSourceproductType.DataSource = productType.getproductTypeAll();
            comboBoxptype.DataSource = bindingSourceproductType;
            comboBoxptype.DisplayMember = "ptype";
            comboBoxptype.ValueMember = "ptype";

            bindingSourceProductList.DataSource = product.getproductListAll();
            dGridProductList.DataSource = bindingSourceProductList;
            DcolumnSet.colSet(moduleInfo.productList, dGridProductList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridProductList.RowCount.ToString() + "条";

            dGridProductList.ReadOnly = false;

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.productList );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                buttImport.Enabled = modulAaccessRow.importAc;

            }
        }
        //工具栏查询按钮,查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {

            FrmDataQuery query = new FrmDataQuery(moduleInfo.productList);
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridProductList.DataSource = query.rTable;
            }
        }
     
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
             state = "编辑";
            FrmProductAdd productAdd = new FrmProductAdd(bindingSourceProductList,state);
            productAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string pname = dGridProductList.Rows[dGridProductList.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除产品：" + pname + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    bindingSourceProductList.DataSource = product.productListDel(pname);
                    dGridProductList.DataSource = bindingSourceProductList;
                    DcolumnSet.colSet(moduleInfo.productList, dGridProductList);
                    toolStripStatusLabel1.Text = "数据总条数:" + dGridProductList.RowCount.ToString() + "条";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            //bindingSourceProductList.DataSource = product.getproductListAll();
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
            try
            {
                baseSet.Query q=new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
                bindingSourceProductList.DataSource = q.sqlQueryLikeAllToTable(moduleInfo.productList,textPnameQuery.Text.ToString().Trim());
                dGridProductList.DataSource = bindingSourceProductList ;
                //DcolumnSet.colSet(moduleInfo.productList, dGridProductList);
                dGridProductList.ReadOnly = false;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            bindingSourceproductType.DataSource = productType.getproductTypeAll();
            comboBoxptype.DataSource = bindingSourceproductType;
            comboBoxptype.DisplayMember = "ptype";
            comboBoxptype.ValueMember = "ptype";

            bindingSourceProductList.DataSource = product.getproductListAll();
            dGridProductList.DataSource = bindingSourceProductList;
            DcolumnSet.colSet(moduleInfo.productList, dGridProductList);
            toolStripStatusLabel1.Text = "数据总条数:" + dGridProductList.RowCount.ToString() + "条";
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)  
        {
            this.Close();
        }
        private void buttImport_Click(object sender, EventArgs e)
        {
            FrmImport import = new FrmImport( moduleInfo.productList );
            import.ShowDialog();
        }
        //datagridview第一列显示行号
        //private void dGridProductList_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        //{
        //    e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1); 
        //}
        //combox控件的选中属性
        private void comboBoxptype_SelectedIndexChanged(object sender, EventArgs e)
        {

          bindingSourceProductList .DataSource =  product.getproductListAllByptype(comboBoxptype .Text);
          dGridProductList.DataSource = bindingSourceProductList;
        }
        //打印按钮
        private void toolPrint_Click(object sender, EventArgs e)
        {
            DataSet s = new DataSet();
            moduleListBLL module = new moduleListBLL();
            productListBLL product = new productListBLL();
            s.Tables.Add(product.getproductListAll());
            s.Tables.Add(module.getmoduleListAll());
            basicForm.FrmPrint print = new basicForm.FrmPrint((object)s, moduleInfo.productList);
            print.ShowDialog();

        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            //moduleInfo.productList
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.productList, dGridProductList, "产品信息");
            frm.ShowDialog();
        }

        private void dGridProductList_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            //for (int i = 0; i < e.RowCount; i++)
            //{
            //    dGridProductList.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            //    dGridProductList.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            //}

            //for (int i = e.RowIndex + e.RowCount; i < this.dGridProductList.Rows.Count; i++)
            //{
            //    dGridProductList.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            //    dGridProductList.Rows[i].HeaderCell.Value = (i + 1).ToString();
            //}  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingSourceProductList.Filter = "pname like '%" + textPnameQuery.Text.ToString() + "%'";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingSourceProductList.Filter = "";
        }

        private void textPnameQuery_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //MessageBox.Show("执行");
                buttonQuery_Click(this, e);
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void dGridProductList_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dGridProductList.Columns["mark"].Index)
            {
                //执行更新语句
                MessageBox.Show("我已经修改到数据库了");
            }
        }

        private void dGridProductList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            baseSet.datGridSet.gridViesSet.setGridViewRowNum(e, dGridProductList);
        }

        private void toolSet_Click(object sender, EventArgs e)
        {

        }
    }
}
